/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.misena.praxispraktik.recursos;


import com.misena.praxispraktik.entity.Personas;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

/**
 *
 * @author diana
 */
@Stateless
@Path("/persona")
public class PersonasRest {

    Personas persona;
    EntityManagerFactory emf = null;
    EntityManager em = null;

    @GET
    @Produces("application/json")
    public Personas consultar(@QueryParam("id") Integer id) {
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            persona = em.find(Personas.class, id);
                        
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        return persona;
    }

    @POST
    @Consumes("application/json")
    public Response guardar(Personas a) {
        persona = a;
        
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            em.persist(a);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        return Response.ok().build();
    }

    /**
     *
     * @param a
     * @return
     */
    @PUT
    @Consumes("application/json")
    public Response actualizar(Personas a) {
        persona = a;
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            em.merge(a);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        return Response.ok().build();
    }

    @DELETE
    @Consumes("application/json")
    public Response Eliminar(@QueryParam("id") Integer id) {

        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            persona = em.find(Personas.class, id);

            if (!persona.getIdPersonas().equals(null)) {
                em.remove(persona);
            } else {
                return Response.noContent().build();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        return Response.ok().build();
    }

    public EntityManagerFactory obtenerEntityManagerFactory() {

        return Persistence.createEntityManagerFactory("PraxisPraktikPU");
    }

    public EntityManager obtenerEntityManager(EntityManagerFactory emf) {

        return emf.createEntityManager();
    }
}
